<?php

/**
 * 圆通快递查询接口
 * @author chenyun
 */
namespace App\Service\VendorClient\OrderLogisticSearchApi;

use App\Service\Code\Normal;
use App\Service\BaseService;
use function GuzzleHttp\json_encode;
use App\Service\TaskServer\TaskWorker;
use Service\AppService\IdService;
use function GuzzleHttp\json_decode;

class YtoLogisticSearchApiClient extends BaseService
{

    private $account = 'yto';

    private $config = [];

  /**
     * 初始参数
     * @param obj $collection
     */
    public function __construct( $apiBody = [] ) 
    {
        $this->config = $apiBody;
    }
    /**
     * 查询物流单号
     * 
     * @param string $express_sn            
     */
    
    public function QueryLogisticByExpressSn($params = [])
    {
        $param = [
            'NUMBER' => $params["express_sn"],
        ];
        $param = json_encode($param, true);
        
        $requestData = [
            'timestamp' => time(),
            'param' => $param,
            'sign' => base64_encode(md5($param . "track_query_adapter" . $this->config["version"] . $this->config['key'], true)),
            'format' => 'JSON'
        ];
        
        $requestParam = [
            'method' => 'QueryLogisticByExpressSn',
            'data' => $requestData
        ];
        return $this->execute($requestParam);
    }

    /**
     * CURL请求
     * 
     * @param
     *            $params
     * @return mixed
     */
    public function execute($param = [])
    {
        if (empty($param['data'])) {
            return Normal::returnCode(Normal::FAIL, "数据不能为空");
        }
        
        $method = $param['method'];
        $url = $this->config['queryExpressSnApi'];
        
        $parameter = [
            [
                'tag' => $this->account . '_' . $method,
                'uid' => IdService::instance()->getOtherID(),
                'url' => $url,
                'data' => $param['data'],
                'method' => 'post',
                'format' => 'json',
                'header' => [
                    "Content-Type: application/json"
                ]
            ]
            
        ];
        
        $resTask = TaskWorker::instance()->addTask($parameter);
        $resTask = current($resTask);
        $res     = $resTask['response']['result'];
        $resHttp = @json_decode($res, true);
        //判断是否成功请求返回值
        if( strpos($res, 'waybill_No') !== false ) {
            $data = $this->getLogisticLists($resHttp);
            return Normal::returnCode(Normal::SUC, "操作成功", $data);
        }
        $message = isset($resHttp['reason']) ? $resHttp['reason'] : '请求失败';
        return Normal::returnCode(Normal::FAIL, $message);
    }

    /**
     * 组装数据
     * 
     * @param array $param = []
     */
    public function getLogisticLists( $logisticList = [] )
    {
        $arr = [];
        foreach ($logisticList as $item) {
            $arr[] = [
                'time' => $item['upload_Time'],
                'context' => $item['processInfo']
            ];
        }
        //根据时间从重新排序
        $edit  = array_column($arr,'time');
        array_multisort($edit,SORT_DESC,$arr);
        return $arr;
    }
}
